<template>
  <!-- 客户/市场信息 -->
  <div>
    <curd-title v-if="infoFlg" :title="'客户/市场信息'" />
    <avue-crud
      :table-loading="customerLoading"
      :option="customerOption"
      :data="customerList"
      @on-load="onCustomerLoad"
      @row-save="rowCustomerSave"
      @row-update="rowCustomerUpdate"
      @refresh-change="refreshCustomerChange"
      @row-del="rowCustomerDel"
      :row-class-name="delRowClassName"

    >
      <template slot="mainCustomer" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.mainCustomerFlag === '1'">{{ scope.row.mainCustomer }}</del>
          {{ scope.row.mainCustomerA }}
        </div>
        <span v-else>{{ scope.row.mainCustomer }}</span>
      </template>
      <template slot="beginDate" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.beginDateFlag === '1'">{{ scope.row.beginDate }}</del>
          {{ scope.row.beginDateA }}
        </div>
        <span v-else>{{ scope.row.beginDate }}</span>
      </template>
      <template slot="salesPreviousYear" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.salesPreviousYearFlag === '1'">{{ scope.row.salesPreviousYear }}</del>
          {{ scope.row.salesPreviousYearA }}
        </div>
        <span v-else>{{ scope.row.salesPreviousYear }}</span>
      </template>
      <template slot="salesLastYear" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.salesLastYearFlag === '1'">{{ scope.row.salesLastYear }}</del>
          {{ scope.row.salesLastYearA }}
        </div>
        <span v-else>{{ scope.row.salesLastYear }}</span>
      </template>
      <template slot="ratio" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.ratioFlag === '1'">{{ scope.row.ratio }}</del>
          {{ scope.row.ratioA }}
        </div>
        <span v-else>{{ scope.row.ratio }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { customerOption } from '@/option/tenant/supplier/factory-info'
import { getStore } from '@/util/store'
import { getCustomerList, removeCustomer, addCustomer, updateCustomer } from '@/api/supplier/customer'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'CustomerInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      // 客户/市场信息
      customerLoading: false,
      customerList: [],
      customerOption: customerOption.call(this)
    }
  },
  methods: {
    onCustomerLoad() {
      this.infoFlg
        ? ((this.customerLoading = true),
          getCustomerList({ supplierId: this.supplierId }).then(res => {
            this.customerList = res.data.data
            this.customerLoading = false
          }))
        : ''
    },
    refreshCustomerChange() {
      this.onCustomerLoad()
    },
    rowCustomerSave(row, done, loading) {
      addCustomer({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onCustomerLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowCustomerUpdate(row, index, done, loading) {
      updateCustomer(row).then(
        () => {
          this.onCustomerLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowCustomerDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeCustomer(row.id)
        })
        .then(() => {
          this.onCustomerLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
